package binarysearch.test162;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/11 12:14
 */
public class Solution2 {

    // 二分查找
    public int findPeakElement(int[] nums) {
        if (nums.length == 1) return 0;
        int l = 0, r = nums.length - 1;
        if (nums[l] > nums[l + 1]) return l;
        if (nums[r] > nums[r - 1]) return r;
        while (l < r) {
            int mid = l + ((r - l) >> 1);
            if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
                return mid;
            } else if (nums[mid] < nums[mid + 1]) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        return l;
    }
}
